import {
    Message
} from "element-ui";
import moment from "moment";
import AIP_CONFIG from '@/request';

export default {
    install(Vue) {
        // 全局保存
        Vue.prototype.handleSave = function (componentsList) {
            Message.info('保存中')
            // console.log( moment().format('YYYY-MM-DD HH:mm:ss'))
            const timer = moment().format('YYYY-MM-DD HH:mm:ss')
            // const draggerlist = this.$store.state.componentsList;
            const currentItemConfig = this.$store.state.currentItemConfig;
            this.$store.commit("addcomponentsList",componentsList);
            this.$store.commit('currentItemConfig', { ...currentItemConfig, time: timer })
            componentsList = componentsList.map(it => {
                return {
                    ...it,
                    isEditTitle:false,
                    focused:false
                }
            })
            this.httpServer({
                url: AIP_CONFIG.MAIN.updateByDtoReturnId,
                method: 'post'
            }, {
                pageContent: JSON.stringify(componentsList),
                canvas: JSON.stringify({
                    canvasW: currentItemConfig.width,
                    canvasH: currentItemConfig.height,
                }),
                id: currentItemConfig.kfid,
                pageName:currentItemConfig.pageName
            }).then(res => {
                Message.success('保存成功')
                // window..postMessage({save:true},document.referrer + '#/led-manage')
            }).catch(err => {
                Message.error('保存失败--->' + err)
            })

        }

        // 解析模板字符串
        Vue.prototype.analysisTemplate = function (interfaceInfo, str, res) {
            // console.log(str.match(/\{\{[^"]*\}\}/g))
            let strin = str
            interfaceInfo.map(k => {
                k.tagOption.map(it => {

                    if (str.indexOf(`{{${it.title}}}`) !== -1 && res[it.key]) {
                        strin = strin.replaceAll(`{{${it.title}}}`, it.key);
                        strin = strin.replaceAll(it.key, res[it.key])
                        // strin = strin.replaceAll("}}", "");
                    }
                })
            })
          
            return strin

        }

    }
}

